package com.ydd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.CateListDTO;
import com.ydd.entity.Cate;
import com.ydd.mapper.CateMapper;
import com.ydd.service.ICateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.vo.CateListVO;
import com.ydd.vo.CateRowInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@Service
public class CateServiceImpl extends ServiceImpl<CateMapper, Cate> implements ICateService {

    @Autowired
    CateMapper cateMapper;
    @Override
    public CateListVO Page(CateListDTO cateListDTO) {
        PageHelper.startPage(cateListDTO.getPage(),cateListDTO.getLimit());
        List<Cate> list= cateMapper.page(cateListDTO.getCatename());
        Page catePage=(Page) list;
        return CateListVO.builder()
                .count(catePage.getTotal())
                .totalPages(catePage.getPages())
                .pageSize(catePage.getPageSize())
                .currentPage(catePage.getPageNum())
                .data(catePage.getResult())
                .build();
    }

    /**
     * 分类列表-全部
     * @return
     */
    @Override
    public List<CateRowInfoVO> all() {
        List<Cate> cates = cateMapper.selectList(null);
        List<CateRowInfoVO> list = new ArrayList<>();
        cates.forEach(cate -> {
            CateRowInfoVO cateRowInfoVO = new CateRowInfoVO();
            BeanUtils.copyProperties(cate,cateRowInfoVO);
            list.add(cateRowInfoVO);
        });
        return list;
    }
}
